package com.hspedu.filecopy;

import java.io.*;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/12
 * @time 8:31
 */
public class FileClassify {

    public static void main(String[] args) {
        try {
            fileCopyImgBed();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void fileCopyGeekTime(){
        String pdfDestPath = "D:\\BaiduNetdiskDownload\\极客时间\\10 【完结】JAVA并发编程实战\\PDF";
        String mp3DestPath = "D:\\BaiduNetdiskDownload\\极客时间\\10 【完结】JAVA并发编程实战\\mp3";
        String parentPath = "D:\\BaiduNetdiskDownload\\极客时间\\10 【完结】JAVA并发编程实战";
        File file = new File(parentPath);
        String[] fileNames = file.list();
        for (String fileName : fileNames) {
            if (fileName.contains(".pdf")){
                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                try {
                    bis = new BufferedInputStream(new FileInputStream(parentPath + "\\" + fileName));
                    bos = new BufferedOutputStream(new FileOutputStream(pdfDestPath+"\\"+fileName));

                    int readLen = 0;
                    byte[] buf = new byte[1024];
                    while ((readLen = bis.read(buf)) != -1){
                        bos.write(buf,0,readLen);
                    }

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally{
                    if (bis != null){
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    if (bos != null){
                        try {
                            bos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

                File srcFile = new File(parentPath + "\\" + fileName);
                srcFile.delete();

            }else if (fileName.contains(".mp3")){

                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                try {
                    bis = new BufferedInputStream(new FileInputStream(parentPath + "\\" + fileName));
                    bos = new BufferedOutputStream(new FileOutputStream(mp3DestPath+"\\"+fileName));

                    int readLen = 0;
                    byte[] buf = new byte[1024];
                    while ((readLen = bis.read(buf)) != -1){
                        bos.write(buf,0,readLen);
                    }

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally{
                    if (bis != null){
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    if (bos != null){
                        try {
                            bos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                File srcFile = new File(parentPath + "\\" + fileName);
                srcFile.delete();
            }
        }

    }

    public static void fileCopyImgBed() throws IOException {
        String parent = "D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\My笔记\\test\\";
;        File directory = new File(parent);
        String[] files = directory.list();
        String filePath = files[0];
        String fileCompletePath = parent + filePath;
        File file = new File(fileCompletePath);
        BufferedReader br = new BufferedReader(new FileReader(fileCompletePath));
        byte[] buf = new byte[1024];
        String lineContent = null;
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\My笔记\\test\\表的相关操作2.md"));
        while ((lineContent = br.readLine()) != null ){
            lineContent.replace("gitee.com/gu-qihao","gu-qihao.oss-cn-beijing.aliyuncs.com");
            bw.write(lineContent);
        }
        bw.flush();

        if (bw != null){
            bw.close();
        }

        if (br != null){
            br.close();
        }
    }
}
